package context

import (
	"database/sql"
	"fmt"
	"strings"

	"github.com/jmoiron/sqlx"
)

type Connection interface {
	Exec(sql string, args ...any) (sql.Result, error)
	Select(dest any, sql string, args ...any) error
	showSql(sql string, args ...any)
}

type MysqlConnection struct {
	DB *sqlx.DB
}

func (c *MysqlConnection) Exec(sql string, args ...any) (sql.Result, error) {
	prepare, _ := c.DB.Prepare(sql)

	return prepare.Exec(args...)
}

func (c *MysqlConnection) Select(dest any, sql string, args ...any) error {
	// ctx.showSql(sql, args...)
	err := c.DB.Select(dest, sql, args...)
	if err != nil {
		return err
	}
	return nil
}

func (c *MysqlConnection) showSql(sql string, args ...any) {
	fmt.Printf(strings.Replace(sql+"\n", "?", "%v", -1), args...)
}
